<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="queryParams" ref="queryParamsRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="手术间号" prop="roonNo">
          <el-input
            v-model="queryParams.roonNo"
            placeholder="请输入手术间号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手术间位置" prop="location">
          <el-input
            v-model="queryParams.location"
            placeholder="请输入手术间位置"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态是否启用" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态是否启用" clearable class="width-120">
            <el-option
              v-for="dict in dictDataMap['sys_job_status']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="handleAdd"
        >
          <template
            #icon>
            <i-ep-plus/>
          </template>
          新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :data="surgeryRoomList" :border="true" :stripe="true"
              highlight-current-row :header-cell-style="$headerCellStyle">
      <el-table-column label="手术室" :align="'center'" prop="surgeryName"/>
      <el-table-column label="手术间" :align="'center'" prop="roonNo"/>
      <el-table-column label="手术间位置" :align="'center'" prop="location"/>
      <el-table-column align="center" label="启用状态" prop="status">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_job_status']" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" :align="'center'" prop="remarks"/>
      <el-table-column label="操作" :align="'center'" class-name="small-padding fixed-width" width="220">
        <template #default="scope">
          <el-button
            type="success"
            @click="handleUpdate(scope.row)"
          >
            <template #icon>
              <i-ep-edit/>
            </template>
            修改
          </el-button>
          <el-button
            type="danger"
            @click="handleDelete(scope.row)"
          >
            <template #icon>
              <i-ep-delete/>
            </template>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手术间维护对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="手术室" prop="surgeryId">
          <dept-list
            :dept-attribute-str="'12'"
            :dept-id="form.surgeryId"
            :org-id="form.orgId"
            :placeholder="'请选择手术室'"
            style="width: 100%"
            @deptChange="deptChange"
          />
        </el-form-item>
        <el-form-item label="手术间号" prop="roonNo">
          <el-input v-model="form.roonNo" placeholder="请输入手术间号"/>
        </el-form-item>
        <el-form-item label="手术间位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入手术间位置"/>
        </el-form-item>
        <el-form-item label="状态是否启用">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dictDataMap['sys_job_status']"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "SurgeryRoom"
}
</script>

<script setup lang="ts">
import {
  addSurgeryRoom,
  delSurgeryRoom,
  getSurgeryRoom,
  selectPageSurgeryRoom,
  updateSurgeryRoom
} from "@/api/surgery/surgeryRoom";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {useUserStoreHook} from "@/store/modules/user.ts";
import DeptList from "_c/Dept/deptList.vue";

const {proxy} = getCurrentInstance() as any;


const dictStore = useDictStoreHook();
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = useUserStore.user.userInfo;
let dictDataMap = ref(dictStore.isTypeGetData('sys_job_status'))

const formRef = ref();
const queryParamsRef = ref()
const surgeryRoomList = ref([]);
const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const form = ref<any>({
  roonNo: "",
  surgeryId: "",
  location: "",
  status: "",
  orgId: userInfo.orgId,
  createByName: "",
  updateByName: "",
  remarks: ""
})
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  roonNo: "",
  surgeryId: "",
  location: "",
  status: "",
  orgId: userInfo.orgId,
  createByName: "",
  updateByName: "",
  remarks: ""
})
const rules = ref<any>({
  roonNo: [
    {
      required: true, message: "手术间号不能为空", trigger: "blur"
    }
  ],
})


/** 查询手术间维护列表 */
function getList() {
  selectPageSurgeryRoom(queryParams.value).then((response: any) => {
    surgeryRoomList.value = response.rows;
    total.value = response.total;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    surgeryRoomId: "",
    roonNo: "",
    surgeryId: "",
    location: "",
    status: "0",
    orgId: userInfo.orgId
  }
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  // proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.surgeryRoomId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  if (!queryParams.value.orgId) {
    return ElMessage.error("请先选择机构")
  }
  open.value = true;
  reset();
  title.value = "添加手术间维护";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  reset();
  const _surgeryRoomId = row.surgeryRoomId || ids.value
  getSurgeryRoom(_surgeryRoomId).then((response: any) => {
    form.value = response.data;
    title.value = "修改手术间维护";
  });
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      if (form.value.surgeryRoomId) {
        updateSurgeryRoom(form.value).then(() => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      } else {
        addSurgeryRoom(form.value).then(() => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _surgeryRoomIds = row.surgeryRoomId ||
    ids.value;
  ElMessageBox.confirm('是否确认删除手术间数据？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delSurgeryRoom(_surgeryRoomIds).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  })
}

/**
 * 手术室选择事件
 * @param obj
 */
function deptChange(obj: any) {
  form.value.surgeryId = obj.deptId
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/surgeryRoom/export', {
    ...queryParams.value
  }, `surgeryRoom_${new Date().getTime()}.xlsx`)
}

getList();
</script>
